﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Text;

public partial class _Default : System.Web.UI.Page 
{
    List<Person> personlist;
    List<string> txtlist;

    protected void btnDo_Click(object sender, EventArgs e)
    {
        this.txtlist = GetContent();



        this.personlist = new List<Person>();

        //ClientScript.RegisterStartupScript(Page.GetType(), "e", "alert('1')", true);

        foreach (string s in txtlist)
        {
            if (!(s.Contains("\"") || s.Contains(",")))//Case 1: without , and "
            {
                this.personlist.Add(GetPerson(s, false));
            }
            else if (s.Contains("\""))//Case 3,4 with ""
            {
                List<string> temps=s.Split(',').ToList();
                for(int i=0;i<temps.Count;i++)
                {
                    if (temps[i] == null||(!temps[i].Contains('\"')))
                    {
                        continue;
                    }

                    int num = temps[i].ToString().ToCharArray().Where(c=>c=='\"').Count();//该段中双引号数量

                    while (num % 2 != 0)
                    {
                        temps[i] += ("," + temps[i + 1]);
                        temps[i + 1] = null;
                        num = temps[i].ToString().ToCharArray().Where(c => c == '\"').Count();
                    }

                    string test = temps[i];
                    test = removebeginend(temps[i]);
                    test=test.Replace("\"\"", "\"");
                    temps[i] = test;
                }

                temps.Remove(null);

                #region 创建person
                Person p = new Person();
                p.Id = Convert.ToInt32(temps[0]);
                p.Name = temps[1];
                p.Like = temps[2];
                p.Points = (float)Convert.ToDouble(temps[3]);

                try
                {
                    p.Brithday = Convert.ToDateTime(temps[4]);
                }
                catch
                {
                    p.Brithday = DateTime.MinValue;
                }

                this.personlist.Add(p);
                #endregion

            }
            else//Case 2, normal
            {
                this.personlist.Add(GetPerson(s, true));
            }
        }

        grdResult.DataSource = personlist;
        grdResult.DataBind();
    }

    private string removebeginend(string input)
    {
        if (input.StartsWith("\"") && (input.EndsWith("\"")))//如果以“”“”起终，则去掉首位各一个“
        {
            return input.Substring(1,input.Length-2);
        }
        else
        {
            return input;
        }
    }

    private List<string> GetContent()
    {
        List<string> contentlist = new List<string>();

        try
        {
            using (StreamReader sr = new StreamReader(Server.MapPath(this.txtPath.Text.Trim())))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    contentlist.Add(line);
                }
                return contentlist;
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            return null;
        }
    }

    private Person GetPerson(string info, bool isnormal)
    {
        if (isnormal)
        {
            Person p = new Person();
            //to do
            string[] infos = info.Split(',');

            try
            {
                p.Id = Convert.ToInt32(infos[0]);
                p.Name = infos[1];
                p.Like = infos[2];
                p.Points = (float)Convert.ToDouble(infos[3]);

                try
                {
                    p.Brithday = Convert.ToDateTime(infos[4]);
                }
                catch
                {
                    p.Brithday = DateTime.MinValue;
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message+info);
                return null;
            }
            return p;
        }
        else
        {
            Person p = new Person();
            p.Id = 0;
            p.Name = info;
            p.Like = "";
            p.Points = 0;
            p.Brithday = DateTime.MinValue;
            return p;
        }
    }
}
